package com.example.itas;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.Toast;

import com.example.classes.ReviewInfo;

public class EvaluationActivity extends Activity implements OnClickListener{

	ProgressDialog dialog; 
	
	private EditText title_edt;
	private EditText review_edt;
	private Button review_commit_btn;
	private RatingBar myRating;
	private float numStars;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_evaluation);
		
		title_edt = (EditText) findViewById(R.id.review_title_edt);
		review_edt = (EditText) findViewById(R.id.review_review_edt);
		review_commit_btn = (Button) findViewById(R.id.review_commit_btn);
		myRating = (RatingBar) findViewById(R.id.ratingBar1);
		
		myRating.setOnRatingBarChangeListener(new OnRatingBarChangeListener(){

			public void onRatingChanged(RatingBar ratingBar, float rating,
					boolean fromUser) {
				// TODO Auto-generated method stub
				numStars = rating;
				
			}
		});
		
		review_commit_btn.setOnClickListener(this);
		
		
	}
	
	public void onClick(View v)
	{
		if(v.equals(review_commit_btn)){
		AsyncReview al = new AsyncReview();
		al.execute();
		}
	}
	
	public ReviewInfo makeReview(){
		String title = title_edt.getText().toString();
		String content = review_edt.getText().toString();
		float num_Stars = numStars;
		
		ReviewInfo tempInfo = new ReviewInfo(title, content, num_Stars);
		return tempInfo;
	}
	
	private class AsyncReview extends AsyncTask<String, Void, String> {

		@Override
		protected void onPreExecute() {

			dialog = ProgressDialog.show(EvaluationActivity.this, "",
					"Now Uploading...", true);
		}

		@Override
		protected String doInBackground(String... params) {

			ReviewContainerActivity.getContainer().add(makeReview());
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			dialog.dismiss();
			Toast.makeText(EvaluationActivity.this, "Review success!",
					Toast.LENGTH_SHORT).show();
			Intent intent = new Intent(EvaluationActivity.this, ReviewContainerActivity.class);
			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(intent); 
			finish();
			
		}
	}


}
